home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tcsh / dist / sh.hist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-21  |  5.4 KB  |  225 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.01/RCS/sh.hist.c,v 3.2 1991/10/12 04:23:51 christos Exp $ */
  2. /*
  3.  * sh.hist.c: Shell history expansions and substitutions
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "sh.h"
  38.  
  39. RCSID("$Id: sh.hist.c,v 3.2 1991/10/12 04:23:51 christos Exp $")
  40.  
  41. #include "tc.h"
  42.  
  43. extern bool histvalid;
  44. extern Char histline[];
  45. static Char HistLit = 0;
  46.  
  47. static    void    hfree    __P((struct Hist *));
  48. static    void    dohist1    __P((struct Hist *, int *, int, int, int));
  49. static    void    phist    __P((struct Hist *, int, int));
  50.  
  51. /*
  52.  * C shell
  53.  */
  54.  
  55. void
  56. savehist(sp)
  57.     struct wordent *sp;
  58. {
  59.     register struct Hist *hp, *np;
  60.     register int histlen = 0;
  61.     Char   *cp;
  62.  
  63.     /* throw away null lines */
  64.     if (sp->next->word[0] == '\n')
  65.     return;
  66.     cp = value(STRhistory);
  67.     if (*cp) {
  68.     register Char *p = cp;
  69.  
  70.     while (*p) {
  71.         if (!Isdigit(*p)) {
  72.         histlen = 0;
  73.         break;
  74.         }
  75.         histlen = histlen * 10 + *p++ - '0';
  76.     }
  77.     }
  78.     for (hp = &Histlist; np = hp->Hnext;)
  79.     if (eventno - np->Href >= histlen || histlen == 0)
  80.         hp->Hnext = np->Hnext, hfree(np);
  81.     else
  82.         hp = np;
  83.     (void) enthist(++eventno, sp, 1);
  84. }
  85.  
  86. struct Hist *
  87. enthist(event, lp, docopy)
  88.     int     event;
  89.     register struct wordent *lp;
  90.     bool    docopy;
  91. {
  92.     register struct Hist *np;
  93.  
  94.     np = (struct Hist *) xmalloc((size_t) sizeof(*np));
  95.     (void) time(&(np->Htime));
  96.     np->Hnum = np->Href = event;
  97.     if (docopy) {
  98.     copylex(&np->Hlex, lp);
  99.     if (histvalid)
  100.         np->histline = Strsave(histline);
  101.     else
  102.         np->histline = NULL;
  103.     }
  104.     else {
  105.     np->Hlex.next = lp->next;
  106.     lp->next->prev = &np->Hlex;
  107.     np->Hlex.prev = lp->prev;
  108.     lp->prev->next = &np->Hlex;
  109.     np->histline = NULL;
  110.     }
  111.     np->Hnext = Histlist.Hnext;
  112.     Histlist.Hnext = np;
  113.     return (np);
  114. }
  115.  
  116. static void
  117. hfree(hp)
  118.     register struct Hist *hp;
  119. {
  120.  
  121.     freelex(&hp->Hlex);
  122.     if (hp->histline)
  123.     xfree((ptr_t) hp->histline);
  124.     xfree((ptr_t) hp);
  125. }
  126.  
  127. /*ARGSUSED*/
  128. void
  129. dohist(vp, c)
  130.     Char  **vp;
  131.     struct command *c;
  132. {
  133.     int     n, rflg = 0, hflg = 0, tflg = 0;
  134.  
  135.     if (getn(value(STRhistory)) == 0)
  136.     return;
  137.     if (setintr)
  138. #ifdef BSDSIGS
  139.     (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
  140. #else
  141.     (void) sigrelse(SIGINT);
  142. #endif
  143.     while (*++vp && **vp == '-') {
  144.     Char   *vp2 = *vp;
  145.  
  146.     while (*++vp2)
  147.         switch (*vp2) {
  148.         case 'h':
  149.         hflg++;
  150.         break;
  151.         case 'r':
  152.         rflg++;
  153.         break;
  154.         case 't':
  155.         tflg++;
  156.         break;
  157.         case '-':        /* ignore multiple '-'s */
  158.         break;
  159.         default:
  160.         stderror(ERR_HISTUS);
  161.         break;
  162.         }
  163.     }
  164.     if (*vp)
  165.     n = getn(*vp);
  166.     else {
  167.     n = getn(value(STRhistory));
  168.     }
  169.     dohist1(Histlist.Hnext, &n, rflg, hflg, tflg);
  170. }
  171.  
  172. static void
  173. dohist1(hp, np, rflg, hflg, tflg)
  174.     struct Hist *hp;
  175.     int    *np, rflg, hflg, tflg;
  176. {
  177.     bool    print = (*np) > 0;
  178.  
  179.     for (; hp != 0; hp = hp->Hnext) {
  180.     (*np)--;
  181.     hp->Href++;
  182.     if (rflg == 0) {
  183.         dohist1(hp->Hnext, np, rflg, hflg, tflg);
  184.         if (print)
  185.         phist(hp, hflg, tflg);
  186.         return;
  187.     }
  188.     if (*np >= 0)
  189.         phist(hp, hflg, tflg);
  190.     }
  191. }
  192.  
  193. static void
  194. phist(hp, hflg, tflg)
  195.     register struct Hist *hp;
  196.     int     hflg, tflg;
  197. {
  198.     struct tm *t;
  199.     char    ampm = 'a';
  200.  
  201.     if (hflg == 0) {
  202.     xprintf("%6d\t", hp->Hnum);
  203.     if (tflg == 0) {
  204.         t = localtime(&hp->Htime);
  205.         if (adrof(STRampm)) {    /* addition by Hans J. Albertsson */
  206.         if (t->tm_hour >= 12) {
  207.             if (t->tm_hour > 12)
  208.             t->tm_hour -= 12;
  209.             ampm = 'p';
  210.         }
  211.         else if (t->tm_hour == 0)
  212.             t->tm_hour = 12;
  213.         xprintf("%2d:%02d%cm\t", t->tm_hour, t->tm_min, ampm);
  214.         }
  215.         else {
  216.         xprintf("%2d:%02d\t", t->tm_hour, t->tm_min);
  217.         }
  218.     }
  219.     }
  220.     if (HistLit && hp->histline)
  221.     xprintf("%s\n", short2str(hp->histline));
  222.     else
  223.     prlex(&hp->Hlex);
  224. }
  225.